#!/bin/bash
set -x

# 检查 /usr/local/src/start_hadoop_lock.d 是否已经存在
lockFile="/usr/local/src/start_hadoop_lock.d"
if [ ! -e "$lockFile" ]; then
    # 未存在执行 初始化依赖安装
    echo 'start_hadoop_lock not exists'>> /usr/local/src/installLog.log
    echo '--------------hostSeting--------------'>> /usr/local/src/installLog.log
    echo '172.20.0.2 hadoop1' >> /etc/hosts
    echo '172.20.0.3 hadoop2' >> /etc/hosts
    echo '172.20.0.4 hadoop3' >> /etc/hosts
    # 安装gcc
    echo '--------------gcc install--------------'>> /usr/local/src/installLog.log
    yum -y install gcc gcc-c++ automake autoconf libtool make
    # 安装tcl
    echo '--------------tcl install--------------'>> /usr/local/src/installLog.log
    cd /usr/local/src/tcl8.6.12/unix/
    ./configure --prefix=/usr/tcl --enable-shared
    make
    make install
    # 目录tcl8.6.12/unix/下的tclUnixPort.h复制到generic中
    cp tclUnixPort.h ../generic/
    # 安装expect
    echo '--------------expect install--------------'>> /usr/local/src/installLog.log
    cd /usr/local/src/expect5.45.4
    ##配置expect的安装路径，--with-tcl为告诉configure脚本tcl解释器位置，--with-tclinclude为告诉脚本tcl头文件位置
    ./configure --prefix=/usr/expect --with-tcl=/usr/tcl/lib --with-tclinclude=../tcl8.6.12/generic
    make
    make install
    ln -s /usr/tcl/bin/expect /usr/expect/bin/expect
    # 授权允许普通用户使用
    echo '--------------pre set--------------'>> /usr/local/src/installLog.log
    chmod -R 755 /usr/expect
    chmod 755 /usr/expect/bin
    chmod  755 /usr/tcl/lib/expect5.45.4
    chmod -R 755 /usr/tcl
    # 脚本执行授权
    chmod +x /usr/local/src/sshKeygen.exp
    chmod +x /usr/local/src/ssh-copy.exp
    chmod +x /usr/local/src/starthadoop.sh
else
    echo 'start_hadoop_lock exists'>> /usr/local/src/installLog.log
fi
echo '--------------start ssh--------------'>> /usr/local/src/installLog.log
/usr/sbin/sshd -D &
echo '--------------start jar--------------'>> /usr/local/src/installLog.log
nohup java -jar /usr/local/src/hadoopDocker-start.jar
echo '--------------end entrypoint--------------'>> /usr/local/src/installLog.log